package org.linlinjava.litemall.db.service;

import org.linlinjava.litemall.db.dao.AgedmallUserInfoMapper;
import org.linlinjava.litemall.db.dao.app.AppAgentMapper;
import org.linlinjava.litemall.db.domain.AgedmallUserInfo;
import org.linlinjava.litemall.db.domain.AgedmallUserInfoExample;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;

@Service
public class AgedmallUserInfoService {
    @Resource
    private AgedmallUserInfoMapper agedmallUserInfoMapper;
    @Resource
    private AppAgentMapper appAgentMapper;

    public AgedmallUserInfo getById(Integer id) {
        return agedmallUserInfoMapper.selectByPrimaryKey(id);
    }

    public AgedmallUserInfo getByUid(Integer userId) {
        AgedmallUserInfoExample example = new AgedmallUserInfoExample();
        example.or().andDeletedEqualTo(false)
                .andUserIdEqualTo(userId);
        return agedmallUserInfoMapper.selectOneByExampleWithBLOBs(example);
    }

    public void subtractAgentAmount(Integer userId, BigDecimal amount) {
        appAgentMapper.addAgentAmount(userId, amount.multiply(new BigDecimal(-1)));
    }

    public void add(Integer userId) {
        AgedmallUserInfo agedmallUserInfo = new AgedmallUserInfo();
        agedmallUserInfo.setUserId(userId);
        agedmallUserInfo.setAgentAmount(BigDecimal.ZERO);
        agedmallUserInfo.setDeductAmount(BigDecimal.ZERO);
        agedmallUserInfo.setDeleted(false);
        agedmallUserInfo.setAddTime(LocalDateTime.now());
        agedmallUserInfo.setUpdateTime(LocalDateTime.now());
        agedmallUserInfoMapper.insertSelective(agedmallUserInfo);
    }

    public void addDeductAmount(Integer userId, BigDecimal amount) {
        appAgentMapper.addDeductAmount(userId, amount);
    }
}
